package com.cyl.h5.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.h5.config.SecurityUtil;
import com.cyl.manager.act.domain.entity.FhMypunctuation1;
import com.cyl.manager.act.domain.oFhIndexPostinfo;
import com.cyl.manager.act.domain.entity.FhMypunctuation;
import com.cyl.manager.act.domain.query.FhMypunctuationQuery;
import com.cyl.manager.act.mapper.FhMypunctuationMapper;
import com.cyl.manager.act.mapper.oFhIndexPostinfoMapper;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 我的标点Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class H5FhMypunctuationService {

    @Autowired
    private FhMypunctuationMapper fhMypunctuationMapper;

    @Autowired
    private oFhIndexPostinfoMapper fhIndexPostinfoMapper; ;

    /**
     * 查询我的标点
     *
     * @param id 我的标点主键
     * @return 我的标点
     */
    public FhMypunctuation1 selectById(Integer id) {

        FhMypunctuation fhMypunctuation=fhMypunctuationMapper.selectById(id);

        FhMypunctuation1 arrrr = new FhMypunctuation1();
        arrrr.setId(fhMypunctuation.getId());
        arrrr.setAddress(fhMypunctuation.getAddress());
        arrrr.setCrateTime(fhMypunctuation.getCrateTime());
        arrrr.setLongitude(fhMypunctuation.getLongitude());
        arrrr.setLatitude(fhMypunctuation.getLatitude());
        arrrr.setUserId(fhMypunctuation.getUserId());
        arrrr.setName(fhMypunctuation.getName());

        oFhIndexPostinfo fhIndexPostinfo = new oFhIndexPostinfo();
        fhIndexPostinfo.setLongitude(fhMypunctuation.getLongitude());
        fhIndexPostinfo.setLatitude(fhMypunctuation.getLatitude());
        fhIndexPostinfo.setDelFlag(0);

        List<oFhIndexPostinfo> fhIndexPostinfoList = fhIndexPostinfoMapper.selectFhIndexPostinfoList(fhIndexPostinfo);
        arrrr.setFhIndexPostinfoList(fhIndexPostinfoList);

        return arrrr;
    }

    /**
     * 查询我的标点列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 我的标点
     */
    public List<FhMypunctuation1> selectList(FhMypunctuationQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber()+1, page.getPageSize());
        }
        Long memberId = SecurityUtil.getLocalUserInfo().getId();
        QueryWrapper<FhMypunctuation> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        String nameLike = query.getNameLike();
        if (!StringUtils.isEmpty(nameLike)) {
            qw.like("name", nameLike);
        }
        String longitude = query.getLongitude();
        if (!StringUtils.isEmpty(longitude)) {
            qw.eq("longitude", longitude);
        }
        String latitude = query.getLatitude();
        if (!StringUtils.isEmpty(latitude)) {
            qw.eq("latitude", latitude);
        }
        LocalDateTime crateTime = query.getCrateTime();
        if (crateTime != null) {
            qw.eq("crate_time", crateTime);
        }
        String address = query.getAddress();
        if (!StringUtils.isEmpty(address)) {
            qw.eq("address", address);
        }
        Long userId = memberId;
        if (userId != null) {
            qw.eq("user_id", userId);
        }
        Long status = 1L;
        if (status != status) {
            qw.eq("status", status);
        }
        String timeSortType = query.getTimeSortType();
        if (!StringUtils.isEmpty(timeSortType)) {
            if ("asc".equalsIgnoreCase(timeSortType)) {
                qw.orderByAsc("crate_time");
            } else if ("desc".equalsIgnoreCase(timeSortType)) {
                qw.orderByDesc("crate_time");
            }
        }
        Integer isNearby = query.getIsNearby();
        List<FhMypunctuation> fhMypunctuationList = fhMypunctuationMapper.selectList(qw);
        //PageHelper.startPage(0, 15);
        List<FhMypunctuation1> resultList = new ArrayList<>();
            if (fhMypunctuationList!=null&&fhMypunctuationList.size()>0) {
                for (FhMypunctuation punctuation : fhMypunctuationList) {
                        FhMypunctuation1 arrrr = new FhMypunctuation1();
                        arrrr.setId(punctuation.getId());
                        arrrr.setAddress(punctuation.getAddress());
                        arrrr.setCrateTime(punctuation.getCrateTime());
                        arrrr.setLongitude(punctuation.getLongitude());
                        arrrr.setLatitude(punctuation.getLatitude());
                        arrrr.setUserId(punctuation.getUserId());
                        arrrr.setName(punctuation.getName());
                        arrrr.setStatus(punctuation.getStatus());

                        oFhIndexPostinfo fhIndexPostinfo = new oFhIndexPostinfo();
                        fhIndexPostinfo.setLongitude(punctuation.getLongitude());
                        fhIndexPostinfo.setLatitude(punctuation.getLatitude());
                        fhIndexPostinfo.setDelFlag(0);
                        if (isNearby!=null&&isNearby==1) {
                            List<oFhIndexPostinfo> fhIndexPostinfoList = fhIndexPostinfoMapper.selectFhIndexPostinfoList(fhIndexPostinfo);
                            arrrr.setFhIndexPostinfoList(fhIndexPostinfoList);
                        }
                        resultList.add(arrrr);
                    }
            }
        return resultList;


    }
    /**
     * 将实体对象转换为 Map
     *
     * @param obj 实体对象
     * @return 包含对象属性的 Map
     */
    private Map<String, Object> convertToMap(Object obj) {
        Map<String, Object> map = new HashMap<>();
        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(obj.getClass());
        for (PropertyDescriptor descriptor : propertyDescriptors) {
            String propertyName = descriptor.getName();
            if (!"class".equals(propertyName)) {
                Method readMethod = descriptor.getReadMethod();
                if (readMethod != null) {
                    try {
                        Object value = readMethod.invoke(obj);
                        map.put(propertyName, value);
                    } catch (Exception e) {
                        // 记录日志，方便排查问题
                        System.err.println("Failed to get value for property " + propertyName + ": " + e.getMessage());
                    }
                }
            }
        }
        return map;
    }
    /**
     * 新增我的标点
     *
     * @param fhMypunctuation 我的标点
     * @return 结果
     */
    public FhMypunctuation insert(FhMypunctuation fhMypunctuation) {
        Long memberId = SecurityUtil.getLocalUserInfo().getId();
        fhMypunctuation.setUserId(memberId.intValue());
        fhMypunctuation.setCrateTime(LocalDateTime.now());
        fhMypunctuationMapper.insert(fhMypunctuation);
        FhMypunctuation fhMypunctuation1 = fhMypunctuationMapper.selectById(fhMypunctuation.getId());
        return fhMypunctuation1;
    }

    /**
     * 修改我的标点
     *
     * @param fhMypunctuation 我的标点
     * @return 结果
     */
    public int update(FhMypunctuation fhMypunctuation) {
        return fhMypunctuationMapper.updateById(fhMypunctuation);
    }

    /**
     * 批量删除我的标点
     *
     * @param ids 需要删除的我的标点主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return fhMypunctuationMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除我的标点信息
     *
     * @param id 我的标点主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return fhMypunctuationMapper.updateDelFlagByIds(ids);
    }
}
